﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XLSupermarket.Financial.Wages
{
    public partial class Send_E_mailForm : Form
    {
        public Send_E_mailForm()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string rec = this.textBox1.Text;
            string email = rec + this.comboBox1.Text;
            string sub = this.textBox2.Text;
            string filename = this.textBox4.Text;
            string con = this.textBox3.Text;
            try
            {
                var emailAcount = "1262737209@qq.com";
                var emailPassword = "pkdnrcmxcitshhfi";
                var reciver = email;
                var content = con;
                MailMessage message = new MailMessage();
                string strAttachfile = @"../../files/"+filename;

                //设置发件人,发件人需要与设置的邮件发送服务器的邮箱一致
                MailAddress fromAddr = new MailAddress("1262737209@qq.com");
                message.From = fromAddr;
                //设置收件人,可添加多个,添加方法与下面的一样
                message.To.Add(reciver);
                //设置抄送人
                message.CC.Add(email);
                //设置邮件标题
                message.Subject = sub;
                //设置邮件内容
                message.Body = content;

                Attachment dataAttachment = new Attachment(strAttachfile);
                message.Attachments.Add(dataAttachment); //附件需要用Add的方式增加到邮件中

                //设置邮件发送服务器,服务器根据你使用的邮箱而不同,可以到相应的 邮箱管理后台查看,下面是QQ的
                SmtpClient client = new SmtpClient("smtp.qq.com", 25);
                //设置发送人的邮箱账号和密码
                client.Credentials = new NetworkCredential(emailAcount, emailPassword);
                //启用ssl,也就是安全发送
                client.EnableSsl = true;
                //发送邮件
                client.Send(message);
                MessageBox.Show("发送成功");
                Clear();
            }
            catch (Exception)
            {
                MessageBox.Show("发送失败,请查看是否输入正确");
            }
        }

        private void Send_E_mailForm_Load(object sender, EventArgs e)
        {
            this.comboBox1.SelectedIndex = 0;
        }

        public void Clear() {
            this.textBox1.Text = "";
            this.comboBox1.SelectedIndex = 0 ;
            this.textBox2.Text = "";
            this.textBox4.Text = "";
            this.textBox3.Text = "";
        }

    }
}
